<?php

namespace App\Http\Controllers\Cruise\Feishu;

use App\Http\Controllers\BaseController;
use App\Models\Cruise\CruiseOrderModel;
use App\Service\Cruise\Check\CruiseCheckAlbumService;
use App\Service\OpenShop\SiteLocation\AuthService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use OSS\Core\OssException;

class CruiseCheckAlbumController extends BaseController
{
    /**
     * 稽核相册文件上传
     * @throws GuzzleException
     * @throws OssException
     * @throws ValidationException
     */
    public function uploadAlbum(Request $request): JsonResponse
    {
        $this->validate($request, [
            'type'       => 'required|int',  //0-图片  1-视频
            'file'       => 'required|string',
            'address'    => 'string',
            'relationId' => 'required|int',
            'module'     => 'int',
            'cruiseOrderId'     => 'int',  // 与任务关联
        ]);

        $module   = (int)$request->post('module', CruiseOrderModel::TYPE_CHECK);
        $authInfo = AuthService::getRmsUserInfo($request);
        $isAlbum  = (int)$request->get('isAlbum', 1);//是否进入稽核相册 默认1-是

        // 如果进入稽核相册  必须传递任务ID
        if ($isAlbum && !$request->cruiseOrderId) {
            return $this->fail(4001, '进入稽核相册的文件必须关联任务');
        }

        $res = CruiseCheckAlbumService::uploadAlbum((int)$request->cruiseOrderId, $request->type, $request->file, $request->relationId, $module, $authInfo, (string)$request->address, $isAlbum);
        if(!$isAlbum){
            return $this->success($res);
        }
        return $this->success();
    }

    /**
     * @throws ValidationException
     */
    public function list(Request $request): JsonResponse
    {
        $this->validate($request, [
            'cruiseOrderId' => 'required|int|gt:0',
            'relationId' => 'required|int',
            'module'     => 'required|int',
            'status'     => 'sometimes',  //null-全部  0-未使用  1-已使用 2-已过期
        ], [
            'cruiseOrderId.required' => '需要与任务绑定的参数',
            'cruiseOrderId.gt'       => '需要与任务绑定的参数',
        ]);
        return $this->success(CruiseCheckAlbumService::list($request->cruiseOrderId, $request->relationId, $request->status, $request->module));

    }

}
